package red.stu.mc.mod.codeblock.util;

import java.awt.Color;

import net.minecraft.block.material.MapColor;

public class MapColorUtil {

	private static final int MAX_COLOR_LEN = 51 + 1;
	public static RGB[] colors = new RGB[MAX_COLOR_LEN];

	static {
		int i = 3 + 1; // 1.2.3 透明
		colors[i + 0] = new RGB(88, 123, 38);
		colors[i + 1] = new RGB(107, 151, 47);
		colors[i + 2] = new RGB(125, 175, 55);
		colors[i + 3] = new RGB(66, 93, 29);
		colors[i + 4] = new RGB(171, 161, 113);
		colors[i + 5] = new RGB(210, 198, 138);
		colors[i + 6] = new RGB(243, 229, 160);
		colors[i + 7] = new RGB(128, 121, 85);
		colors[i + 8] = new RGB(138, 138, 138);
		colors[i + 9] = new RGB(168, 168, 168);
		colors[i + 10] = new RGB(196, 196, 196);
		colors[i + 11] = new RGB(103, 103, 103);
		colors[i + 12] = new RGB(117, 0, 0);
		colors[i + 13] = new RGB(217, 0, 0);
		colors[i + 14] = new RGB(251, 0, 0);
		colors[i + 15] = new RGB(133, 0, 0);
		colors[i + 16] = new RGB(110, 110, 177);
		colors[i + 17] = new RGB(136, 136, 217);
		colors[i + 18] = new RGB(157, 157, 251);
		colors[i + 19] = new RGB(83, 83, 133);
		colors[i + 20] = new RGB(115, 115, 115);
		colors[i + 21] = new RGB(142, 142, 142);
		colors[i + 22] = new RGB(164, 164, 164);
		colors[i + 23] = new RGB(87, 87, 87);
		colors[i + 24] = new RGB(0, 86, 0);
		colors[i + 25] = new RGB(0, 104, 0);
		colors[i + 26] = new RGB(0, 122, 0);
		colors[i + 27] = new RGB(0, 64, 0);
		colors[i + 28] = new RGB(177, 177, 177);
		colors[i + 29] = new RGB(217, 217, 217);
		colors[i + 30] = new RGB(251, 251, 251);
		colors[i + 31] = new RGB(133, 133, 133);
		colors[i + 32] = new RGB(113, 116, 127);
		colors[i + 33] = new RGB(139, 142, 156);
		colors[i + 34] = new RGB(161, 165, 181);
		colors[i + 35] = new RGB(85, 87, 95);
		colors[i + 36] = new RGB(104, 75, 53);
		colors[i + 37] = new RGB(128, 93, 65);
		colors[i + 38] = new RGB(149, 107, 76);
		colors[i + 39] = new RGB(78, 56, 39);
		colors[i + 40] = new RGB(78, 78, 78);
		colors[i + 41] = new RGB(94, 94, 94);
		colors[i + 42] = new RGB(110, 110, 110);
		colors[i + 43] = new RGB(58, 58, 58);
		colors[i + 44] = new RGB(44, 44, 177);
		colors[i + 45] = new RGB(54, 54, 217);
		colors[i + 46] = new RGB(63, 63, 251);
		/*hsvs[47] = new HSV();
		hsvs[48] = new HSV();
		hsvs[49] = new HSV();
		hsvs[50] = new HSV();
		hsvs[51] = new HSV();*/
	}

	public static int getMCColor(int color) {
		Color c = new Color(color);
		return getMCColor(c.getRed(), c.getGreen(), c.getBlue());
	}

	public static int getMCColor(int r, int g, int b) {
		int minRGB = 765;
		int resultColor = 0;
		for (int i = 1; i < colors.length; i++) {
			if (colors[i] == null)
				continue;
			int temp = (Math.abs(r - colors[i].r) 
	        		+ Math.abs(g - colors[i].g) 
	        		+ Math.abs(b - colors[i].b)) / 3;
			if (temp < minRGB) {
				minRGB = temp;
				resultColor = i;
			}
		}
		return resultColor;
	}

}

